package com.erikpasternak.vader;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class PouringFragment extends Fragment implements View.OnClickListener {
	private VaderActivity.VaderStateListener mListener;
    private DrinkPourHelper mPourHelper;
    private Button mNext;
    private Button mCancel;

    public PouringFragment() {
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
        VaderActivity va = (VaderActivity) activity;
        mListener = va.getListener();
        mPourHelper = va.getDrinkPourHelper();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle bundle) {
        View view = inflater.inflate(R.layout.pouring, parent, false);
        mNext = (Button) view.findViewById(R.id.next);
        mNext.setOnClickListener(this);

        mCancel = (Button) view.findViewById(R.id.cancel);
        mCancel.setOnClickListener(this);

        float cost = mPourHelper.getCost();
        if (cost != 0) {
            TextView costView = (TextView) view.findViewById(R.id.payment);
            costView.setText("Please see the bartender for final price. Estimated $" + cost
                    + " for your drink.");
        }
		return view;
	}

	public void onClick(View v) {
        if (v.getId() == R.id.cancel) {
            mPourHelper.cancel();
			mListener.onStateChange(VaderActivity.STATE_INITIAL);
		} else {
            mListener.onStateChange(VaderActivity.STATE_WAITING);
            mPourHelper.sendDrink();
            mNext.setEnabled(false);
		}
	}
}
